import Cookies from 'js-cookie';
import { listMenu} from "@/api/setting/menu";
import { handleTree} from "@/utils/methodConf";
const state = {
    sidebar: {
        opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    },
    //权限菜单树
    menuOptions:[]
}

const mutations = {
    CLOSE_SIDEBAR: (state) => {
        Cookies.set('sidebarStatus', 0)
        state.sidebar.opened = false
    },
    OPEN_SIDEBAR: (state) => {
        Cookies.set('sidebarStatus', 1)
        state.sidebar.opened = true
    },
    TOGGLE_SIDEBAR: state => {
        state.sidebar.opened = !state.sidebar.opened
        if (state.sidebar.opened) {
            Cookies.set('sidebarStatus', 1)
        } else {
            Cookies.set('sidebarStatus', 0)
        }
    },
    ADD_MENUTREE:(state,data) =>{
        state.menuOptions = data
    }
}

const actions = {
    CloseSideBar({ commit }) {
        commit('CLOSE_SIDEBAR')
    },
    ToggleSideBar({ commit }) {
        commit('TOGGLE_SIDEBAR')
    },
    CreateMenuTree({ commit }){
        listMenu().then(response => {
            const data = handleTree(response.data, "menuId");
            commit('ADD_MENUTREE',data);
        });
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
